package com.ruoyi.fly.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.fly.domain.Store;
import com.ruoyi.fly.domain.vo.*;
import com.ruoyi.fly.service.StoreProductService;
import com.ruoyi.fly.service.StoreServerService;
import com.ruoyi.fly.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/store")
@Api(tags = "店铺管理")
public class StoreController extends BaseController {

    @Autowired
    private StoreService storeService;

    @Autowired
    private StoreProductService storeProductService;

    @Autowired
    private StoreServerService storeServerService;


    /**
     * 查询店铺列表
     *
     * @return
     */
    @ApiOperation(value = "查询店铺列表")
    @PostMapping("/selStoreList")
    public TableDataInfo selectStoreList(@RequestBody StoreSearchVo store) {
        //startPage无效，添加新分页
        PageDomain pageDomain = new PageDomain();
        BeanUtils.copyProperties(store,pageDomain);
        startPage(pageDomain);
        List<StoreVo> list = storeService.selectStoreList(store);
        return getDataTable(list);
    }

    @ApiOperation(value = "查询店铺列表")
    @PostMapping("/selectList")
    public R selectList(@RequestBody StoreSearchVo store) {
        List<StoreVo> list = storeService.selectList(store);
        List<BackVo> backVoList = new ArrayList<>();
        for (StoreVo storeVo : list) {
            BackVo backVo = new BackVo();
            backVo.setId(storeVo.getId());
            backVo.setValue(storeVo.getStoreName());
            backVoList.add(backVo);
        }
        return R.ok(backVoList);
    }

    /**
     * 查询店铺详情
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "查询店铺详情")
    @GetMapping("/selStore/{id}")
    public R selectStoreById(@PathVariable("id") String id) {
        return R.ok(storeService.selectStoreById(id));
    }

    /**
     * 增加店铺
     * @param store
     * @return
     */
    @ApiOperation(value = "增加店铺")
    @PostMapping(value = "/applyStore")
    public R applyStore(@RequestBody Store store) {
        return storeService.insertStore(store);
    }

    /**
     * 店铺审核
     * @param dto
     * @return
     */
    @ApiOperation(value = "店铺审核")
    @PostMapping(value = "/examineStore")
    public R examineStore(@RequestBody Examine dto) {
        return storeService.examineStore(dto);
    }


    /**
     * 修改店铺
     * @param store
     * @return
     */
    @ApiOperation(value = "修改店铺")
    @PostMapping("/updateStore")
    public R updateStore(@RequestBody StoreVo store) {
        return storeService.updateStore(store);
    }

    /**
     * 删除店铺
     * @param
     * @return
     */
    @ApiOperation(value = "删除店铺")
    @PostMapping("/delStore")
    public R deleteStore(@RequestBody DeleteVo deleteVo) {
        return storeService.deleteStoreByIds(deleteVo.getIds());
    }

    /**
     * 新增店铺商品
     */
    @ApiOperation(value = "新增店铺商品")
    @PostMapping("/addStoreProduct")
    public R addStoreProduct(@RequestBody StoreProductVo storeProductVo) {
        return storeProductService.insertStoreProduct(storeProductVo);
    }

    /**
     * 修改店铺商品
     */
    @ApiOperation(value = "修改店铺商品")
    @PostMapping("/editStoreProduct")
    public R editStoreProduct(@RequestBody StoreProductVo storeProductVo) {
        return storeProductService.updateStoreProduct(storeProductVo);
    }

    /**
     * 删除店铺商品
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除店铺商品")
    @PostMapping("/deleteStoreProduct/{ids}")
    public R removeStoreProduct(@PathVariable String[] ids) {
        return storeProductService.deleteStoreProductByIds(ids);
    }

    /**
     * 新增店铺服务
     */
    @ApiOperation(value = "新增店铺服务")
    @PostMapping("/addStoreServer")
    public R addStoreServer(@RequestBody StoreServerVo storeServerVo) {
        return storeServerService.insertStoreServer(storeServerVo);
    }

    /**
     * 修改店铺服务
     */
    @ApiOperation(value = "修改店铺服务")
    @PostMapping("/editStoreServer")
    public R editStoreServer(@RequestBody StoreServerVo storeServerVo) {
        return storeServerService.updateStoreServer(storeServerVo);
    }

    /**
     * 删除店铺服务
     */
    @ApiOperation(value = "删除店铺服务")
    @PostMapping("/deleteStoreServer")
    public R removeStoreServer(@RequestBody List<String> ids) {
        return storeServerService.deleteStoreServerByIds(ids);
    }


    /**
     * 删除店铺下的商品或服务
     */
    @ApiOperation(value = "删除店铺下的商品或服务")
    @PostMapping("/deleteStoreProductServer")
    public R deleteStoreProductServer(@RequestBody ProductDeleteVo productDeleteVo) {
        return storeProductService.deleteStoreProductByProductIds(productDeleteVo);
    }
}
